{include file="public/header"/}
<link rel="stylesheet" href="{__PUBLIC_PATH}/admin/pcf/pcfbase.css?v={$version}"/>
<script src="{__PUBLIC_PATH}/common/js/jquery.min.js?v={$version}"></script>
<script src="{__PUBLIC_PATH}/admin/pcf/global.js?v={$version}"></script>
</head>
<body>
<div class="layui-fluid" style="padding-bottom:83px;border:0;" id="LAY-component-layer-list">
<div class="layui-col-sm12">
    <div class="layui-card" style="box-shadow:0 0 0 0;">
      <div class="layui-tab layui-tab-brief" id="pcfsystem">
        <ul class="layui-tab-title" style="background-color:#f2f2f2;">
            <a href="{:url('/system.tools/index')}"><li class="layui-this">数据备份</li></a>
            <a href="{:url('/system.tools/restore')}"><li>数据还原</li></a>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button data-type="export" class="layui-btn icon-btn">开始备份</button>
                        <button data-type="optimize" class="layui-btn icon-btn">数据优化</button>
                        <button data-type="repair" class="layui-btn icon-btn">数据修复</button>
                    </div>
                </div>
            </div>
            <table class="layui-table" id="tableUser" lay-filter="tableUser"></table>
        </div>
       </div>
    </div>
    <div class="pcfclear"></div>
</div>
</div>
{include file="public/footer"/}
<script>
    layui.use(['layer','form','table','admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: "{:url('/system.tools/index')}",
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type:'checkbox'},
                {field: 'Name', title: '数据表',},
                {field: 'Comment', title: '表名'},
                {field: 'Engine', title: '表引擎'},
                {field: 'Rows', title: '记录条数',align: 'center'},
                {field: 'size', title: '占用空间',align: 'center'},
                {field: 'Collation', title: '编码',align: 'center'},
                {field: 'Create_time', title: '创建时间',align: 'center'},
                {field: 'Name',title: '备份状态',align: 'center',templet: function (d) {
                    return "<div id='db"+d.Name+"'>未备份</div>";
                }}
            ]]
        });
        $(document).on("click","#pcfsystem .layui-btn",function(){
          var type = $(this).data('type');
          active[type] && active[type].call(this);
        });
        //触发事件
        var active = {
            //优化
            optimize: function(){
                var checkRows = table.checkStatus('tableUser');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要优化的数据表', {icon: 2,time:1000});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.Name;
                });
                layer.msg('正在处理请勿刷新页面', {icon: 16,shade: 0.01,time:1000000});
                $.ajax({
                    type: "POST",
                    url: "{:url('/system.tools/optimize')}?tablename="+ids,
                    dataType: 'json',
                    success: function (res) {
                        layer.closeAll();
                        if(res.status){
                            layer.msg(res.msg, {icon: 1, time:1000}, function(){
                                window.location.href = res.url;
                            });
                        }else{
                            layer.msg(res.msg, {icon: 2,time:1000});
                        }
                    },
                    error:function(){
                        layer.closeAll();
                        layer.msg(res.msg, {icon: 2,time:1000});
                    }
                });
                return false;
            },
            //修复
            repair: function(){
                var checkRows = table.checkStatus('tableUser');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要修复的数据表', {icon: 2,time:1000});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.Name;
                });
                layer.msg('正在处理请勿刷新页面', {icon: 16,shade: 0.01,time:1000000});
                $.ajax({
                    type: "POST",
                    url: "{:url('/system.tools/repair')}?tablename="+ids,
                    dataType: 'json',
                    success: function (res) {
                        layer.closeAll();
                        if(res.status){
                            layer.msg(res.msg, {icon: 1, time:1000}, function(){
                                window.location.href = res.url;
                            });
                        }else{
                            layer.msg(res.msg, {icon: 2,time:1000});
                        }
                    },
                    error:function(){
                        layer.closeAll();
                        layer.msg(res.msg, {icon: 2,time:1000});
                    }
                });
                return false;
            },
            //备份
            export: function(){

                var tables = [];
                var checkRows = table.checkStatus('tableUser');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要修复的数据表', {icon: 2,time:1000});
                    return;
                }
                var tables = checkRows.data.map(function (d) {
                    return d.Name;
                });

                $export = $(this);
                var disabled = $export.attr('data-disabled');
                if (1 == disabled) {
                    return false;
                }
                $export.addClass("layui-disabled").attr('data-disabled', 1);
                $export.find('span').html("正在发送备份请求...");
                $.post("{:url('/system.tools/export')}",{tables:tables},
                    function(res){
                        if(res.status){
                            tables = res.tables;
                            var loading = layer.msg('正在备份表(<font id="upgrade_backup_table">'+res.tab.table+'</font>)……<font id="upgrade_backup_speed">0.01</font>%', {
                                icon: 1,
                                time: 3600000,//1小时后后自动关闭
                                shade: [0.2]  //0.1透明度的白色背景
                            });
                            $export.find('span').html(res.info);
                            backup(res.tab);
                            window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                        } else {
                            layer.msg(res.info, {icon: 2,time:1500});
                            $export.removeClass("layui-disabled").attr('data-disabled', 0);
                            $export.find('span').html("立即备份");
                        }
                    },"json");
                return false;
                function backup(tab, status){
                    status && showmsg(tab.id, "开始备份……(0%)");
                    $.post("{:url('/system.tools/export')}", tab, function(data){
                        if(data.status){
                            if (tab.table) {
                                showmsg(tab.id, data.info);
                                $('#upgrade_backup_table').html(tab.table);
                                $('#upgrade_backup_speed').html(tab.speed);
                                $export.find('span').html('正在备份……'+tab.speed+'%');
                            } else {
                                $export.find('span').html('进入备份……');
                            }
                            if(!$.isPlainObject(data.tab)){
                                var loading = layer.msg('备份完成……100%，请不要关闭本页面！', {
                                    icon: 1,
                                    time: 2000,  //1小时后后自动关闭
                                    shade: [0.2] //0.1透明度的白色背景
                                });
                                $export.removeClass("layui-disabled").attr('data-disabled', 0);
                                $export.find('span').html("开始备份");
                                setTimeout(function(){
                                    layer.closeAll();
                                    layer.alert('备份成功！', {icon: 1, title:false});
                                }, 1000);
                                window.onbeforeunload = function(){ return null }
                                return;
                            }
                            backup(data.tab, tab.id != data.tab.id);
                        } else {
                            layer.closeAll();
                            $export.removeClass("layui-disabled").attr('data-disabled', 0);
                            $export.find('span').html("立即备份");
                        }
                    }, "json");
                }
                function showmsg(id, msg){
                    $("#pcfsystem").find("#db"+tables[id]).html(msg);
                }
            }
        };
    });
</script>
</body>
</html>